<?php
session_start();
require_once '../config.php';
require_once '../functions.php';

// 检查管理员是否登录
if (!is_admin_logged_in()) {
    header('Location: login.php');
    exit;
}

$message = '';
$message_type = '';

// 添加违禁词
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_word'])) {
    $word = trim($_POST['word'] ?? '');
    
    if (empty($word)) {
        $message = '违禁词不能为空';
        $message_type = 'error';
    } else {
        try {
            // 检查是否已存在
            $stmt = $pdo->prepare("SELECT id FROM prohibited_words WHERE word = ?");
            $stmt->execute([$word]);
            
            if ($stmt->rowCount() > 0) {
                $message = '该词已在违禁词列表中';
                $message_type = 'info';
            } else {
                $stmt = $pdo->prepare("INSERT INTO prohibited_words (word) VALUES (?)");
                $stmt->execute([$word]);
                $message = '添加成功';
                $message_type = 'success';
            }
        } catch (PDOException $e) {
            $message = '添加失败，请重试';
            $message_type = 'error';
        }
    }
}

// 批量添加违禁词
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['batch_add'])) {
    $words_text = trim($_POST['words_text'] ?? '');
    
    if (empty($words_text)) {
        $message = '请输入要添加的违禁词';
        $message_type = 'error';
    } else {
        // 按换行分割
        $words = preg_split('/\r\n|\r|\n/', $words_text);
        $success_count = 0;
        $duplicate_count = 0;
        
        try {
            $pdo->beginTransaction();
            
            foreach ($words as $word) {
                $word = trim($word);
                if (empty($word)) continue;
                
                // 检查是否已存在
                $stmt = $pdo->prepare("SELECT id FROM prohibited_words WHERE word = ?");
                $stmt->execute([$word]);
                
                if ($stmt->rowCount() > 0) {
                    $duplicate_count++;
                    continue;
                }
                
                $stmt = $pdo->prepare("INSERT INTO prohibited_words (word) VALUES (?)");
                $stmt->execute([$word]);
                $success_count++;
            }
            
            $pdo->commit();
            $message = "批量添加完成，成功添加 $success_count 个，重复 $duplicate_count 个";
            $message_type = 'success';
        } catch (PDOException $e) {
            $pdo->rollBack();
            $message = '批量添加失败，请重试';
            $message_type = 'error';
        }
    }
}

// 删除违禁词
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
    $id = $_GET['id'];
    if (is_numeric($id)) {
        $id = (int)$id;
        try {
            $stmt = $pdo->prepare("DELETE FROM prohibited_words WHERE id = ?");
            $stmt->execute([$id]);
            $message = '删除成功';
            $message_type = 'success';
        } catch (PDOException $e) {
            $message = '删除失败，请重试';
            $message_type = 'error';
        }
    } else {
        $message = '参数错误';
        $message_type = 'error';
    }
}

// 清空违禁词
if (isset($_POST['clear_all'])) {
    try {
        $stmt = $pdo->query("DELETE FROM prohibited_words");
        $message = '所有违禁词已清空';
        $message_type = 'success';
    } catch (PDOException $e) {
        $message = '清空失败，请重试';
        $message_type = 'error';
    }
}

// 获取所有违禁词
$stmt = $pdo->query("SELECT * FROM prohibited_words ORDER BY created_at DESC");
$prohibited_words = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>违禁词管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/all.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Arial', sans-serif;
        }
        a {
            text-decoration: none;
            outline: none;
        }
        .sidebar {
            width: 250px; /* 固定宽度 */
            height: 100vh;
            background-color: #ff8fab;
            color: white;
            padding-top: 20px;
            position: fixed;
            overflow-y: auto;
            transition: transform 0.3s ease; /* 添加过渡效果 */
            z-index: 100; /* 确保在内容上方 */
        }
        
        .sidebar-header {
            text-align: center;
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(255,255,255,0.2);
            margin-bottom: 20px;
        }
        
        .sidebar-header h2 {
            margin: 0;
            font-size: 1.5rem;
        }
        
        .nav-link {
            color: white;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }
        
        .nav-link:hover, .nav-link.active {
            background-color: rgba(255,255,255,0.1);
            color: white;
            text-decoration: none;
            border-left-color: #ffd700;
        }
        
        .main-content {
            margin-left: 250px;
            padding: 30px;
            transition: margin-left 0.3s ease; /* 添加过渡效果 */
            min-height: 100vh;
        }
        
        .page-title {
            color: #d63384;
            margin-bottom: 30px;
            font-size: 1.8rem;
        }
        
        .card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            padding: 20px;
            margin-bottom: 30px;
        }
        
        .card-title {
            color: #d63384;
            margin-top: 0;
            margin-bottom: 20px;
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-control {
            width: 100%;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1rem;
        }
        
        .form-control:focus {
            border-color: #ff69b4;
            outline: none;
        }
        
        textarea.form-control {
            min-height: 150px;
            resize: vertical;
        }
        
        .btn {
            padding: 10px 20px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
            font-size: 1rem;
            transition: background-color 0.3s;
        }
        
        .btn-primary {
            background-color: #ff69b4;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #d63384;
        }
        
        .btn-danger {
            background-color: #dc3545;
            color: white;
        }
        
        .btn-danger:hover {
            background-color: #c82333;
        }
        
        .table-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        
        .data-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .data-table th, .data-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        
        .data-table th {
            background-color: #fff0f3;
            color: #d63384;
            font-weight: bold;
        }
        
        .data-table tr:hover {
            background-color: #f9f9f9;
        }
        
        .action-btn {
            padding: 5px 10px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
            font-size: 0.9rem;
            transition: background-color 0.3s;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-delete {
            background-color: #dc3545;
            color: white;
        }
        
        .btn-delete:hover {
            background-color: #c82333;
            color: white;
        }
        
        .message {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .message-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        
        .word-list {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 15px;
        }
        
        .word-tag {
            background-color: #fff0f3;
            color: #d63384;
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 5px;
        }
        
        .word-tag .delete-icon {
            color: #dc3545;
            cursor: pointer;
            font-size: 0.8rem;
        }
        
        .action-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .word-count {
            color: #666;
            font-size: 0.9rem;
        }
        
        .logout-btn {
            margin-top: 20px;
            text-align: center;
        }
        
        .logout-btn a {
            color: white;
            text-decoration: none;
            display: block;
            padding: 10px;
            background-color: rgba(255,255,255,0.1);
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .logout-btn a:hover {
            background-color: rgba(255,255,255,0.2);
        }
        
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                height: auto;
                position: relative;
            }
            
            .main-content {
                margin-left: 0;
                padding: 20px;
            }
            
            .action-bar {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="sidebar col-md-3 col-lg-2 d-md-block">
        <div class="sidebar-header">
            <h2><?php echo SITE_NAME; ?> 管理后台</h2>
        </div>
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link" href="index.php">
                    <i class="fas fa-tachometer-alt"></i> 仪表盘
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="confessions.php">
                    <i class="fas fa-heart"></i> 表白管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="dailies.php">
                    <i class="fas fa-coffee"></i> 日常管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="announcements.php">
                    <i class="fas fa-bullhorn"></i> 公告管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="comments.php">
                    <i class="fas fa-comments"></i> 评论管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link active" href="prohibited_words.php">
                    <i class="fas fa-ban"></i> 违禁词管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="banned_ips.php">
                    <i class="fas fa-shield-alt"></i> 封禁管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="settings.php">
                    <i class="fas fa-cog"></i> 系统设置
                </a>
            </li>
        </ul>
        
        <div class="logout-btn">
            <a href="logout.php">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a>
        </div>
    </div>
    
    <main class="main-content">
        <h1 class="page-title"><i class="fas fa-ban"></i> 违禁词管理</h1>
        
        <?php if (!empty($message)): ?>
            <div class="message message-<?php echo $message_type; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-plus-circle"></i> 添加违禁词</h2>
            <form method="post" action="prohibited_words.php">
                <div class="form-group">
                    <label for="word">单个违禁词：</label>
                    <input type="text" id="word" name="word" class="form-control" placeholder="输入要添加的违禁词">
                </div>
                <button type="submit" name="add_word" class="btn btn-primary"><i class="fas fa-plus"></i> 添加</button>
            </form>
        </div>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-layer-group"></i> 批量添加违禁词</h2>
            <form method="post" action="prohibited_words.php">
                <div class="form-group">
                    <label for="words_text">批量添加（每行一个词）：</label>
                    <textarea id="words_text" name="words_text" class="form-control" placeholder="每行输入一个违禁词，支持批量添加"></textarea>
                </div>
                <button type="submit" name="batch_add" class="btn btn-primary"><i class="fas fa-file-import"></i> 批量添加</button>
            </form>
        </div>
        
        <div class="card">
            <div class="action-bar">
                <h2 class="card-title"><i class="fas fa-list"></i> 违禁词列表</h2>
                <div>
                    <span class="word-count">共 <?php echo count($prohibited_words); ?> 个违禁词</span>
                    <?php if (count($prohibited_words) > 0): ?>
                        <button type="button" class="btn btn-danger" style="margin-left: 10px;" onclick="if(confirm('确定要清空所有违禁词吗？此操作不可恢复！')) document.getElementById('clearForm').submit();">
                            <i class="fas fa-trash"></i> 清空所有
                        </button>
                        <form id="clearForm" method="post" action="prohibited_words.php" style="display: none;">
                            <input type="hidden" name="clear_all" value="1">
                        </form>
                    <?php endif; ?>
                </div>
            </div>
            
            <?php if (count($prohibited_words) > 0): ?>
                <div class="word-list">
                    <?php foreach ($prohibited_words as $word): ?>
                        <div class="word-tag">
                            <?php echo htmlspecialchars($word['word']); ?>
                            <a href="prohibited_words.php?action=delete&id=<?php echo $word['id']; ?>" class="delete-icon" onclick="return confirm('确定要删除这个违禁词吗？')">
                                <i class="fas fa-times"></i>
                            </a>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div style="text-align: center; padding: 30px; color: #666;">
                    暂无违禁词，请添加
                </div>
            <?php endif; ?>
        </div>
    </main>
</body>
</html>
